package pl.edu.agh.agents.gui;

import java.util.Map;

import pl.edu.agh.agents.agent.Agent;
import pl.edu.agh.agents.state.State;

import com.mxgraph.model.mxCell;
import com.mxgraph.view.mxGraph;

public class AgentGraph extends mxGraph {
	private Map<mxCell, State> cellsStates;

	public AgentGraph(Map<mxCell, State> cellsStates) {
		this.cellsStates = cellsStates;

		setCellsEditable(false);
		setCellsResizable(false);
	}

	@Override
	public String getLabel(Object o) {
		if (o != null) {
			mxCell cell = (mxCell)o;

			State state = cellsStates.get(cell);

			// edge
			if (state == null) {
				mxCell target = (mxCell)cell.getTarget();
				State targetState = cellsStates.get(target);
				
				if (targetState != null) {
					return "" + targetState.getEnteringProb();
				}
			}
		}
		
		return super.getLabel(o);
	}

	@Override
	public String getToolTipForCell(Object o) {
		if (o != null) {
			mxCell cell = (mxCell)o;

			State state = cellsStates.get(cell);

			// edge
			if (state == null) {
				return "";
			}

			Agent agent = state.getAgent();

			if (state.isSink()) {
				return "Sink";
			}

			if (agent != null) {
				String goalID = "";
				if (agent.getGoalState() != null) {
					goalID = String.valueOf(agent.getGoalState().getID());
				}
				return "<html>" + agent
						+ "<br/>Goal: " + goalID
						+ "<br/> Ent. prob: " + state.getEnteringProb()
						+ "</html>";
			}


			return "<html>Free"
					+ "<br/>Ent. prob: " + state.getEnteringProb()
					+ "</html>";

		}
		return null;
	}
}